﻿/**  
* FormHistory.cs
*
* 功 能： N/A
* 类 名： FormHistory
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2014/6/29/星期日 21:25:31   N/A    初版
*
* Copyright (c) 2014  Spring Story  Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有： Spring Story 有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using eTruck.IDAL;
using eTruck.DBUtility;
namespace eTruck.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:FormHistory
	/// </summary>
	public partial class FormHistory:IFormHistory
	{
		public FormHistory()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(eTruck.Model.FormHistory model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into FormHistory(");
			strSql.Append("FormID,ProcessID,UserID,Lastupdated)");
			strSql.Append(" values (");
			strSql.Append("@FormID,@ProcessID,@UserID,@Lastupdated)");
			SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4),
					new SqlParameter("@ProcessID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Lastupdated", SqlDbType.DateTime)};
			parameters[0].Value = model.FormID;
			parameters[1].Value = model.ProcessID;
			parameters[2].Value = model.UserID;
			parameters[3].Value = model.Lastupdated;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(eTruck.Model.FormHistory model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update FormHistory set ");
			strSql.Append("FormID=@FormID,");
			strSql.Append("ProcessID=@ProcessID,");
			strSql.Append("UserID=@UserID,");
			strSql.Append("Lastupdated=@Lastupdated");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4),
					new SqlParameter("@ProcessID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Lastupdated", SqlDbType.DateTime)};
			parameters[0].Value = model.FormID;
			parameters[1].Value = model.ProcessID;
			parameters[2].Value = model.UserID;
			parameters[3].Value = model.Lastupdated;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int FormID)
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from FormHistory ");
            strSql.Append(" where FormID=@FormID");
            SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4)
			};
            parameters[0].Value = FormID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public eTruck.Model.FormHistory GetModel()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 FormID,ProcessID,UserID,Lastupdated from FormHistory ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
			};

			eTruck.Model.FormHistory model=new eTruck.Model.FormHistory();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public eTruck.Model.FormHistory DataRowToModel(DataRow row)
		{
			eTruck.Model.FormHistory model=new eTruck.Model.FormHistory();
			if (row != null)
			{
				if(row["FormID"]!=null && row["FormID"].ToString()!="")
				{
					model.FormID=int.Parse(row["FormID"].ToString());
				}
				if(row["ProcessID"]!=null && row["ProcessID"].ToString()!="")
				{
					model.ProcessID=int.Parse(row["ProcessID"].ToString());
				}
				if(row["UserID"]!=null && row["UserID"].ToString()!="")
				{
					model.UserID=int.Parse(row["UserID"].ToString());
				}
				if(row["Lastupdated"]!=null && row["Lastupdated"].ToString()!="")
				{
					model.Lastupdated=DateTime.Parse(row["Lastupdated"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select FormID,ProcessID,UserID,Lastupdated ");
			strSql.Append(" FROM FormHistory ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" FormID,ProcessID,UserID,Lastupdated ");
			strSql.Append(" FROM FormHistory ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM FormHistory ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FormID desc");
			}
			strSql.Append(")AS Row, T.*  from FormHistory T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "FormHistory";
			parameters[1].Value = "FormID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        public DataSet GetHistoryByProcess(string FormID, string ProcessName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select  top 1 a.FormID,b.ProcessName,c.UserName,a.Lastupdated from FormHistory a with(nolock)
                        ,Process b with(nolock),Users c with(nolock)
                         where a.ProcessID=b.ProcessID and a.UserID=c.UserID ");

            strSql.Append(" and a.FormID=@FormID and b.ProcessName='" + ProcessName+"'");//参数不能传中文??
            strSql.Append("  order by a.Lastupdated ");    
            SqlParameter[] parameters = {
					
					new SqlParameter("@FormID",SqlDbType.Int)									
					};
			parameters[0].Value = FormID;
           // parameters[1].Value = ProcessName;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
		#endregion  ExtensionMethod
	}
}

